﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
namespace RobotControl
{
    public partial class Form_Main : Form
    {
        public byte CeJuRxCount = 0;
        double CeJuValue = 0;
        private void CeJuCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte rx;
            if (ComClosing[1])
                return;
            ComListening[1] = true;
            while (CeJuCom.BytesToRead != 0)
            {
                int irx = CeJuCom.ReadByte();
                if (irx == -1)
                    break;
                rx = (byte)irx;
                CeJuRxCount++;
                switch (CeJuRxCount)
                {
                    case 1:
                        if (rx != 0x16)
                            CeJuRxCount = 0;
                        break;
                    case 2:
                        CeJuValue = Convert.ToDouble(rx - '0') * 100;
                        break;
                    case 3:
                        CeJuValue += Convert.ToDouble(rx - '0') * 10;
                        break;
                    case 4:
                        CeJuValue += Convert.ToDouble(rx - '0') * 1;
                        break;
                    case 5:
                        CeJuValue += Convert.ToDouble(rx - '0') * 0.1;
                        break;
                    case 6:
                        if (rx == 0x0d)
                        {
                            ExternChangeParamList(ParamsListAddr.高度, CeJuValue.ToString("F1"));
                            ExternAddText("收到测距声纳数据:" + CeJuValue.ToString("F1") + "米");
                        }
                        CeJuRxCount = 0;
                        break;
                    default:
                        CeJuRxCount = 0;
                        break;
                }
            }
            ComListening[1] = false;
        }
    }
}
